package com.skyolder.crutch.word;

import java.util.ArrayList;

import com.skyolder.crutch.error.GrammarError;
import com.skyolder.crutch.word.token.Token;

public class TokenQueue {
	private ArrayList<Token> tokensBuffer=new ArrayList<Token>();
	private int position=0;
	
	BufferTokenPiper bufferTokenPiper;
	public TokenQueue(BufferTokenPiper bufferTokenPiper){
		this.bufferTokenPiper=bufferTokenPiper;
	}
	public  Token nextToken() throws GrammarError {
		if(tokensBuffer.size()>0){
			position--;
			return tokensBuffer.remove(0);
		}
		return bufferTokenPiper.nextToken();
	}
	public Token peekToken() throws GrammarError{
		//return bufferCharPiper.peekChar();
		if(position<tokensBuffer.size()){
			position++;
			return tokensBuffer.get(position-1);
		}
		Token t=bufferTokenPiper.peekToken();
		tokensBuffer.add(t);
		position++;
		return tokensBuffer.get(position-1);
	}
	public void back(){
		if(position>0){
			position--;
		}
	}
	public void back(int i){
		position-=i;
		if(position<0){
			position=0;
		}
	}
	public void reset(){
		position=0;
	}
}
